function gewCharDescriptor(x,y,w,h,xoffset,yoffset,xadvance)
{
  this.x = x;
  this.y = y;
  this.width = w;
  this.height = h;
  this.xOffset = xoffset;
  this.yOffset = yoffset;
  this.xAdvance = xadvance;
};
function gewFont(name,fontSubTextureName,id)
{
  this.name = name;
  this.lineHeight = 0;
  this.base = 0;
  this.width = 0;
  this.height = 0;
  this.chars = {};
  this.subTextureName = fontSubTextureName;
  this.subTexture = null;
  var font = this;
  var fontDecs = document.getElementById(id);
  if (!fontDecs)
    return null;
  var str = "";
  var k = fontDecs.firstChild;
  while (k) 
  {
    if (k.nodeType == 3)
      str += k.textContent;
    k = k.nextSibling;
  }
  font.parseData(str);
}
gewFont.prototype.getStringWidth= function(str)
{
  var len = str.length;
  var width =0 ;
  for(var i=0;i<len;i++)
  {
    var charDes = this.chars[str[i]];
    if(charDes)
    {
      width+=charDes.xAdvance;
    }
    else
    {
      var charCode = str.charCodeAt(i);
      switch(charCode)
      {
        case 32: // space
          width+= this.chars.a.xAdvance;
          break;
        case 9: // tab
          width+= (this.chars.a.xAdvance*4);
          break;
        default:
          width+= this.chars.a.xAdvance;
          break;        
      }
    }
  }
  return width;
}
gewFont.prototype.generateSprites = function(layer,sprites, str, x,y, depth)
{
  var len = str.length;
  var cursorX = 0;
  var cursorY = 0;
  var subTextureName= this.subTextureName;
  var spriteNum = 0;
  for(var i=0;i<len;i++)
  {
    var charDes = this.chars[str[i]];
    if(charDes)
    {
      var sprite = new gewSprite2D();
      sprite.setAsRectangle(charDes.width,charDes.height);
      sprite.setDepth(depth);
      sprite.setPosition(x+cursorX+ charDes.xOffset,y+cursorY+ charDes.yOffset );
      sprite.setSubTexture(subTextureName);
      sprite.setSubRegion(charDes.x,charDes.y,charDes.width,charDes.height);
      sprites[spriteNum] = sprite;
      spriteNum++;
      layer.insert(sprite);
      cursorX+= charDes.xAdvance;
    }
    else // special char
    {
      var charCode = str.charCodeAt(i);
      switch(charCode)
      {
        case 32: // space
          cursorX+= this.chars.a.xAdvance;
          break;
        case 9: // tab
          cursorX+= (this.chars.a.xAdvance*4);
          break;
        case 10: // \n
          cursorX=0;
          cursorY+=this.lineHeight;
          break;
        default:
          cursorX+= this.chars.a.xAdvance;
          break;        
      }
    }
  }
}
gewFont.prototype.parseData = function(str)
{
  var lines = str.split('\n');
  var reg = /[\S]+/gmi;
  var temp;
  for (var i = 0, len = lines.length; i < len; i++) 
  {
    var line = lines[i];
    reg.compile(reg);
    var token = reg.exec(line);
    if(!token)
      continue;
    if( token[0].localeCompare("info")==0 )
    {
      
    }
    else if( token[0].localeCompare("common")==0 )
    {
      token = reg.exec(line);
      temp = token[0].split('=');
      this.lineHeight = parseInt(temp[1]);
      
      token = reg.exec(line);
      temp = token[0].split('=');
      this.base = parseInt(temp[1]);
      
      token = reg.exec(line);
      temp = token[0].split('=');
      this.width = parseInt(temp[1]);
      
      token = reg.exec(line);
      temp = token[0].split('=');
      this.height = parseInt(temp[1]);
    }
    else if( token[0].localeCompare("char")==0 )
    {
      token = reg.exec(line);
      temp = token[0].split('=');
      var c = String.fromCharCode(parseInt(temp[1]));
      
      token = reg.exec(line);
      temp = token[0].split('=');
      var x = parseInt(temp[1]);
      
      token = reg.exec(line);
      temp = token[0].split('=');
      var y = parseInt(temp[1]);
      
      token = reg.exec(line);
      temp = token[0].split('=');
      var w = parseInt(temp[1]);
      
      token = reg.exec(line);
      temp = token[0].split('=');
      var h = parseInt(temp[1]);
      
      token = reg.exec(line);
      temp = token[0].split('=');
      var xoffset = parseInt(temp[1]);
      
      token = reg.exec(line);
      temp = token[0].split('=');
      var yoffset = parseInt(temp[1]);
      
      token = reg.exec(line);
      temp = token[0].split('=');
      var xadvance = parseInt(temp[1]);
      
      this.chars[c] = new gewCharDescriptor(x,y,w,h,xoffset,yoffset,xadvance);
    }
    else if( token[0].localeCompare("kerning")==0 )
    {
      
    }
  }
}
